In [1]:
import seaborn as sns
import matplotlib.pyplot as plt
%matplotlib inline
from sklearn.datasets import make_blobs
data, labels = make_blobs(n_samples=100, n_features=2, centers=2,cluster_std=4,random_state=2)
plt.scatter(data[:,0], data[:,1], c = labels, cmap='coolwarm');
In [2]:
#Import LinearSVC
from sklearn.svm import LinearSVC
#Create instance of Support Vector Classifier
svc = LinearSVC()
#Fit estimator to 70% of the data
svc.fit(data[:70], labels[:70])
#Predict final 30%
y_pred = svc.predict(data[70:])
#Establish true y values
y_true = labels[70:]
In [3]:
from sklearn.metrics import precision_score
print("Precision score: {}".format(precision_score(y_true,y_pred)))
In [4]:
from sklearn.metrics import recall_score
print("Recall score: {}".format(recall_score(y_true,y_pred)))
In [5]:
from sklearn.metrics import f1_score
print("F1 Score: {}".format(f1_score(y_true,y_pred)))
In [6]:
from sklearn.metrics import classification_report
print(classification_report(y_true,y_pred))
In [7]:
from sklearn.metrics import confusion_matrix
import pandas as pd
confusion_df = pd.DataFrame(confusion_matrix(y_true,y_pred),
columns=["Predicted Class " + str(class_name) for class_name in [0,1]],
index = ["Class " + str(class_name) for class_name in [0,1]])
print(confusion_df)
sklearn.metrics also offers Regression Metrics, Model Selection Scorer, Multilabel ranking metrics, Clustering Metrics, Biclustering metrics, and Pairwise metrics.